﻿using maxiaopeng.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace maxiaopeng.Controllers
{
    public class ChecksController : ApiController
    {
        // GET: api/Checks
        //签到
        [HttpGet]
        public IHttpActionResult up(int UserId, string time)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var year = DateTime.Now.Year;
                var month = DateTime.Now.Month;
                var day = DateTime.Now.Day;
                var start = new DateTime(year, month, day, 0, 0, 0);
                var end = new DateTime(year, month, day, 23, 59, 59);
                var checkuser = db.UserChecks.FirstOrDefault(x => x.UserId == UserId && x.CheckinTime >= start && x.CheckinTime <= end);
                if (checkuser != null)
                {
                    if (checkuser.CheckState == "已签到")
                    {
                        return Json(new { code = -1, msg = "您今天已经签到" });
                    }
                    else
                    {
                        checkuser.CheckState = "已签到";
                        int n = db.SaveChanges();
                        if (n > 0)
                        {
                            return Json(new { code = 0, msg = "签到成功" });
                        }
                        else
                        {
                            return Json(new { code = -1, msg = "签到失败" });

                        }
                    }

                }
                else
                {
                    var username = db.Users.FirstOrDefault(x => x.Id== UserId);
                    UserChecks theuser = new UserChecks();
                    theuser.UserId = UserId;
                    theuser.CheckinTime = DateTime.Now;
                    theuser.CheckState = "已签到";
                    theuser.UserName = username.LoginName;
                    theuser.IsCancel = "未签退";
                    db.UserChecks.Add(theuser);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "签到成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "签到失败" });

                    }
                }
            }
        }
        //签退
        [HttpGet]
        public IHttpActionResult down(int UserId, string time)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var year = DateTime.Now.Year;
                var month = DateTime.Now.Month;
                var day = DateTime.Now.Day;
                var start = new DateTime(year, month, day, 0, 0, 0);
                var end = new DateTime(year, month, day, 23, 59, 59);
                var checkuser = db.UserChecks.FirstOrDefault(x => x.UserId == UserId && x.CheckinTime >= start && x.CheckinTime <= end);
                TimeSpan ts3 = DateTime.Now.Subtract(Convert.ToDateTime(checkuser.CheckinTime)).Duration();
                int a = (int)Math.Round(ts3.TotalMinutes);
                //如果查到今天签到的用户则可以签退 必须在签到后的15分钟后才可以签退
                if (checkuser != null)
                {
                    if (checkuser.CheckState == "已签到")
                    {
                        if (checkuser.IsCancel != "已签退")
                        {
                            //如果签到的时间距离现在时间大于15分钟则可以进行签退
                            if (a > 10)
                            {
                                checkuser.IsCancel = "已签退";
                                checkuser.CheckOutTime = DateTime.Now;
                                int n = db.SaveChanges();
                                if (n > 0)
                                {
                                    return Json(new { code = 0, msg = "签退成功" });
                                }
                                else
                                {
                                    return Json(new { code = 0, msg = "签退失败" });

                                }
                            }
                            else
                            {
                                return Json(new { code = -1, msg = "签到10分钟后才可以进行签退" });
                            }

                        }
                        else
                        {
                            return Json(new { code = -1, msg = "您今天已经签退过了" });
                        }
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "请先进行签到" });
                    }
                }
                else
                {
                    return Json(new { code = -1, msg = "请先进行签到" });
                }
            }

        }
        [HttpGet]
        public IHttpActionResult Checks(int page, int limit, string UserName,string CheckState,string CheckinTime, string CheckOutTime)
        {
            using (CRMDBEntities db = new CRMDBEntities())
            {
                var dataList = db.UserChecks.AsEnumerable();
                var count = dataList.Count();
                if (!string.IsNullOrEmpty(UserName))
                {
                    dataList = dataList.Where(p => p.UserName.Contains(UserName));
                }
                if (!string.IsNullOrEmpty(CheckState))
                {
                    dataList = dataList.Where(p => p.CheckState==CheckState);
                }
                if (!string.IsNullOrEmpty(CheckinTime))
                {
                    dataList = dataList.Where(p => p.CheckinTime>=Convert.ToDateTime(CheckinTime));
                }
                if (!string.IsNullOrEmpty(CheckOutTime))
                {
                    dataList = dataList.Where(p => p.CheckOutTime <= Convert.ToDateTime(CheckOutTime));
                }
                //筛选页面所有符合条件的数据
                var list = dataList.Select(p => new
                {
                    Id = p.Id,
                    UserName = p.UserName,
                    CheckinTime=p.CheckinTime.ToString(),
                    CheckState=p.CheckState,
                    IsCancel=p.IsCancel,
                    CheckOutTime=p.CheckOutTime.ToString(),
                    Weight=p.Weight
                });
                return Json(new { count = count, data = list.OrderByDescending(p => p.Id).Skip((page - 1) * 10).Take(limit).ToList() });

            }
        }
    }
}
